package com.gupaoedu.design.strategy.pro_single.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p></p>
 *
 * @author Andy
 * @date 2018/3/12.
 */
public class BeanFactory {

    private static final Map<String, Object> initParam = new ConcurrentHashMap<String, Object>();

    private BeanFactory(){}

    public static Object getBean(String beanName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        synchronized (BeanFactory.class) {
            if (null == beanName || "".equals(beanName)) {
                throw new RuntimeException("beanName不能为空");
            }

            Object obj = null;
            if (initParam.containsKey(beanName)) {
                obj = initParam.get(beanName);
            } else {
                obj = Class.forName(beanName).newInstance();
                initParam.put(beanName, obj);
            }
            return obj;
        }
    }
}
